package com.zhou.common.operator.log.storage.mybatis.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zhou.common.operator.log.core.OperateLogProperty;
import com.zhou.common.operator.log.core.ao.OperateLogInfoGetPageListAO;
import com.zhou.common.operator.log.core.model.OperateLogInfo;
import com.zhou.common.operator.log.core.service.impl.AbstractStorageService;
import com.zhou.common.operator.log.storage.mybatis.domain.OperateLogInfoConverter;
import com.zhou.common.operator.log.storage.mybatis.domain.OperateLogRecord;
import com.zhou.common.operator.log.storage.mybatis.domain.OperateLogRecordServiceImpl;
import com.zhou.common.pub.model.CommonPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MybatisStorgeService extends AbstractStorageService {
    @Autowired
    private OperateLogRecordServiceImpl operateLogRecordService;

    public MybatisStorgeService(OperateLogProperty property) {
        super(property);
    }

    @Override
    protected void saveBatch(List<OperateLogInfo> logs) {
        List<OperateLogRecord> recordList = new ArrayList();
        logs.stream().forEach((o) -> {
            OperateLogRecord operateLogRecord = new OperateLogRecord();
            operateLogRecord.convertTo(o);
            recordList.add(operateLogRecord);
        });
        this.operateLogRecordService.saveBatch(recordList);
    }

    @Override
    public CommonPage<OperateLogInfo> getPageList(OperateLogInfoGetPageListAO ao) {
        CommonPage<OperateLogInfo> listCommonPage = new CommonPage();
        IPage<OperateLogRecord> page = ao.getPage();
        QueryWrapper<OperateLogRecord> queryWrapper = new QueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(ao.getUserName()), "user_name", ao.getUserName());
        queryWrapper.like(StringUtils.isNotBlank(ao.getResource()), "resource", ao.getResource());
        queryWrapper.like(StringUtils.isNotBlank(ao.getUserPhone()), "user_phone", ao.getUserPhone());
        queryWrapper.eq(StringUtils.isNotBlank(ao.getOperatingSystem()), "operating_system", ao.getOperatingSystem());
        queryWrapper.eq(StringUtils.isNotBlank(ao.getOperatingModule()), "operating_module", ao.getOperatingModule());
        queryWrapper.eq(StringUtils.isNotBlank(ao.getAction()), "action", ao.getAction());
        queryWrapper.like(StringUtils.isNotBlank(ao.getOperatingType()), "operating_type", ao.getOperatingType());
        queryWrapper.like(StringUtils.isNotBlank(ao.getClientIp()), "client_ip", ao.getClientIp());
        queryWrapper.like(StringUtils.isNotBlank(ao.getMacAddress()), "mac_address", ao.getMacAddress());
        if (StringUtils.isNotBlank(ao.getOperTimeStart())) {
            queryWrapper.ge("add_time", ao.getOperTimeStart());
        }

        if (StringUtils.isNotBlank(ao.getOperTimeEnd())) {
            queryWrapper.le("add_time", ao.getOperTimeEnd());
        }

        queryWrapper.orderByDesc("add_time");
        page = this.operateLogRecordService.page(page, queryWrapper);
        if (page != null) {
            listCommonPage.setPageNo(page.getCurrent());
            listCommonPage.setPageSize(page.getSize());
            listCommonPage.setTotalPage(page.getPages());
            listCommonPage.setTotalCount(page.getTotal());
            List<OperateLogInfo> operateLogInfos = new ArrayList();
            if (!CollectionUtils.isEmpty(page.getRecords())) {
                Iterator var6 = page.getRecords().iterator();

                while (var6.hasNext()) {
                    OperateLogRecord operateLogRecord = (OperateLogRecord) var6.next();
                    OperateLogInfo operateLogInfo = OperateLogInfoConverter.OperateLogConveter(operateLogRecord);
                    operateLogInfos.add(operateLogInfo);
                }
            }

            listCommonPage.setList(operateLogInfos);
        }

        return listCommonPage;
    }
}

